<template>
  <div class="tooltip-wrapper" @mouseenter="showTooltip" @mouseleave="hideTooltip">
    <!-- 触发元素 -->
    <div class="tooltip-trigger">
      <slot name="trigger"></slot>
    </div>
    
    <!-- 提示框内容 -->
    <div 
      class="tooltip-content"
      :class="positionClass"
      v-if="visible"
      :style="customStyles"
    >
      <slot></slot>
      <div class="tooltip-arrow" :class="arrowClass"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Tooltip',
  props: {
    // 提示框位置：top, bottom, left, right
    position: {
      type: String,
      default: 'top',
      validator: (value) => {
        return ['top', 'bottom', 'left', 'right'].includes(value);
      }
    },
    // 显示延迟时间(ms)
    delay: {
      type: Number,
      default: 300
    },
    // 自定义样式
    styles: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      visible: false,
      timer: null
    };
  },
  computed: {
    // 根据位置生成对应的样式类
    positionClass() {
      return `tooltip-${this.position}`;
    },
    // 箭头样式类
    arrowClass() {
      return `tooltip-arrow-${this.position}`;
    },
    // 合并默认样式和自定义样式
    customStyles() {
      const defaultStyles = {
        backgroundColor: 'rgba(0, 0, 0, 0.8)',
        color: '#fff',
        padding: '6px 10px',
        borderRadius: '4px',
        fontSize: '12px'
      };
      return { ...defaultStyles, ...this.styles };
    }
  },
  methods: {
    // 显示提示框
    showTooltip() {
      this.timer = setTimeout(() => {
        this.visible = true;
      }, this.delay);
    },
    // 隐藏提示框
    hideTooltip() {
      clearTimeout(this.timer);
      this.visible = false;
    }
  },
  beforeDestroy() {
    // 清除定时器
    clearTimeout(this.timer);
  }
};
</script>

<style scoped>
.tooltip-wrapper {
  position: relative;
  display: inline-block;
}

.tooltip-content {
  position: absolute;
  z-index: 1000;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

/* 位置相关样式 */
.tooltip-top {
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-bottom: 8px;
}

.tooltip-bottom {
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-top: 8px;
}

.tooltip-left {
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-right: 8px;
}

.tooltip-right {
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  margin-left: 8px;
}

/* 箭头样式 */
.tooltip-arrow {
  position: absolute;
  width: 0;
  height: 0;
  border-style: solid;
}

.tooltip-arrow-top {
  top: 100%;
  left: 50%;
  margin-left: -4px;
  border-width: 4px 4px 0;
  border-color: rgba(0, 0, 0, 0.8) transparent transparent;
}

.tooltip-arrow-bottom {
  bottom: 100%;
  left: 50%;
  margin-left: -4px;
  border-width: 0 4px 4px;
  border-color: transparent transparent rgba(0, 0, 0, 0.8);
}

.tooltip-arrow-left {
  top: 50%;
  left: 100%;
  margin-top: -4px;
  border-width: 4px 0 4px 4px;
  border-color: transparent transparent transparent rgba(0, 0, 0, 0.8);
}

.tooltip-arrow-right {
  top: 50%;
  right: 100%;
  margin-top: -4px;
  border-width: 4px 4px 4px 0;
  border-color: transparent rgba(0, 0, 0, 0.8) transparent transparent;
}
</style>
